package com.atguigu.gmall.product.service.impl;

import com.atguigu.gmall.product.service.TestService;
import org.apache.commons.lang.StringUtils;
import org.redisson.api.RLock;
import org.redisson.api.RReadWriteLock;
import org.redisson.api.RedissonClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.core.script.DefaultRedisScript;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.UUID;
import java.util.concurrent.TimeUnit;

/**
 * @author XIN
 * 2023/5/4
 */
@Service
public class TestServiceImpl implements TestService {
    @Autowired
    private StringRedisTemplate stringRedisTemplate;
    @Autowired
    private RedissonClient redissonClient ;
    
    @Override
    public void testLock() {
        RLock lock= redissonClient.getLock("lock");
        lock.lock(10,TimeUnit.SECONDS);
        try {
            String s = stringRedisTemplate.opsForValue().get("num");
            if (StringUtils.isBlank(s)){
                return;
            }
            int i = Integer.parseInt(s);
            stringRedisTemplate.opsForValue().set("num", String.valueOf(++i));
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            lock.unlock();
        }
    }
    
    @Override
    public String read() {
        RReadWriteLock readWriteLock = redissonClient.getReadWriteLock("readWriteLock");
        RLock lock = readWriteLock.readLock();
        lock.lock(10,TimeUnit.SECONDS);
        String msg = stringRedisTemplate.opsForValue().get("msg");
        return msg;
    }
    
    @Override
    public void write() {
        RReadWriteLock readWriteLock = redissonClient.getReadWriteLock("readWriteLock");
        RLock lock = readWriteLock.writeLock();
        lock.lock(10,TimeUnit.SECONDS);
        stringRedisTemplate.opsForValue().set("msg", "msgData");
    }
    
    public void testLock1() {
        String uuid = UUID.randomUUID().toString();
        Boolean flag = stringRedisTemplate.opsForValue().setIfAbsent("lock", uuid,2, TimeUnit.SECONDS);
        System.out.println(flag);
        if (flag) {
            String value = stringRedisTemplate.opsForValue().get("num");
            if (StringUtils.isBlank(value)) {
                return;
            }
            int i = Integer.parseInt(value);
            stringRedisTemplate.opsForValue().set("num", String.valueOf(++i));
            DefaultRedisScript<Long> defaultRedisScript = new DefaultRedisScript<>();
            String script = "if redis.call(\"get\",KEYS[1]) == ARGV[1]\n" +
                    "then\n" +
                    "    return redis.call(\"del\",KEYS[1])\n" +
                    "else\n" +
                    "    return 0\n" +
                    "end";
            defaultRedisScript.setScriptText(script);
            defaultRedisScript.setResultType(Long.class);
            stringRedisTemplate.execute(defaultRedisScript, Arrays.asList("lock", uuid));
        }else {
            try {
                //睡眠
                Thread.sleep(100);
                //自旋重试
                this.testLock();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
//        if (uuid.equals(stringRedisTemplate.opsForValue().get("lock"))){
//            this.stringRedisTemplate.delete("lock");
//            }
//            stringRedisTemplate.delete("lock");
//        }else {
//            try {
//                Thread.sleep(100);
//            this.testLock();
//            } catch (InterruptedException e) {
//                e.printStackTrace();
//            }
        }
         
    }
//    @Override
//    public synchronized void testLock() {
//        String num = (String) redisTemplate.opsForValue().get("num");
//        if (StringUtils.isEmpty(num)){
//            this.redisTemplate.opsForValue().set("num", "0");
//            return;
//        }
//        int i = Integer.parseInt(num);
//        this.redisTemplate.opsForValue().set("num", String.valueOf(++i));
//    }
}
